import { PrismaClient } from '@prisma/client';
import { NextResponse } from 'next/server';
import { STATUS_CODES, STATUS_MESSAGES } from '@/lib/constants/statusCode';

const prisma = new PrismaClient();

export async function POST(request) {
  try {
    const { address } = await request.json();
    
    if (!address) {
      return NextResponse.json(
        { error: '钱包地址不能为空' },
        { status: STATUS_CODES.BAD_REQUEST }
      );
    }

    // 创建用户
    const user = await prisma.user.upsert({
      where: { address: address.toLowerCase() },
      update: { nonce: crypto.randomUUID() },
      create: {
        address: address.toLowerCase(),
        nonce: crypto.randomUUID()
      }
    });

    return NextResponse.json({
        id: user.id,
        address: user.address,
        nonce: user.nonce
    });

  } catch (error) {
    console.error('创建用户失败:', error);
    return NextResponse.json(
      { error: STATUS_MESSAGES[STATUS_CODES.INTERNAL_SERVER_ERROR] },
      { status: STATUS_CODES.INTERNAL_SERVER_ERROR }
    );
  }
}